package com.easpeed.user

import android.app.Activity
import android.content.Intent
import android.graphics.Color
import android.os.Bundle
import android.text.TextUtils
import android.util.Log;
import com.bigyu.utils.Logger
import android.view.View
import androidx.databinding.DataBindingUtil
import androidx.databinding.ObservableField
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.ToastUtils
import com.easpeed.common.CommonConsts
import com.easpeed.comon.consts.ConstsApi
import com.easpeed.comon.memorycache.CommonMemoryCache
import com.easpeed.comon.utils.CommonOperateUtils
import com.easpeed.comon.utils.SocketHelper
import com.easpeed.comon.utils.TcpCode
import com.easpeed.elevator.R
import com.easpeed.elevator.databinding.ActivityUserScanBinding
import com.easpeed.user.utils.zxing.ScanCaptureActivity
import com.easpeed.user.utils.zxing.common.Constant
import com.gyf.immersionbar.ImmersionBar


class UserScanActivity : UserBaseActivity() {

    var binding: ActivityUserScanBinding? = null


    val scanCode = ObservableField<String>("")

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        ImmersionBar.with(this)
            .statusBarDarkFont(false) // 设置状态栏字体为深色
            .transparentStatusBar()  // 透明状态栏
            .fitsSystemWindows(true)
            .statusBarColor(R.color.colorTitleBar)
            .init()
    }

    private fun changeTxt() {
        if (SocketHelper.isConnected()) {
            binding?.connect?.text = "断开连接"
        } else {
            binding?.connect?.text = "连接"
        }
    }

    private var isScanView = false
    override fun initView() {
        binding = DataBindingUtil.setContentView(this, R.layout.activity_user_scan)
        binding?.data = this
        changeTxt()

        scanCode.set(CommonConsts.targetDeviceId)

        binding?.back?.setOnClickListener { finish() }

        binding?.radioGroup?.setOnCheckedChangeListener { _, checkedId ->
            when (checkedId) {
                R.id.device -> {
                    ImmersionBar.with(this)
                        .statusBarDarkFont(false) // 设置状态栏字体为深色
                        .transparentStatusBar()  // 透明状态栏
                        .fitsSystemWindows(true)
                        .statusBarColor(R.color.colorTitleBar)
                        .init()
                    binding?.layoutScan?.visibility = View.GONE
                    binding?.layoutDevice?.visibility = View.VISIBLE
                    binding?.rootLayout?.setBackgroundColor(Color.parseColor("#FF18A0FB"))
                    isScanView = false
                }

                R.id.scan -> {
                    isScanView = true
                    binding?.device?.isChecked = true
                    CommonOperateUtils.moveTo(mContext, ScanCaptureActivity::class.java, requestCode = 1001)
                }
            }
        }

        SocketHelper.addResultListener(javaClass.simpleName) { data ->
            if (data.msg.isNotEmpty()) {
                ToastUtils.showShort(data.msg)
            }
            if (data.code == TcpCode.SUCCESS_SERVER) {
                Logger.e("TAG", "initView: ----------------")
                editor.putString("targetDeviceId", CommonConsts.targetDeviceId)
                editor.commit()
                (mContext as Activity)?.finish()
            }
        }

        binding?.connect?.setOnClickListener {
            if (SocketHelper.isConnected()) {
                SocketHelper.stopSocket()
            } else {
                connectDevice(scanCode.get().toString())
            }
            changeTxt()
        }
        binding?.device?.isChecked = true
    }


    private fun connectDevice(device: String) {
        CommonConsts.targetDeviceId = device
        SocketHelper.startSocket(ConstsApi.socketHost, ConstsApi.socketPort, device)
    }

    override fun onResume() {
        super.onResume()
    }

    override fun onPause() {
        super.onPause()
    }

    override fun onDestroy() {
        SocketHelper.removeResultListener(javaClass.simpleName)
        super.onDestroy()
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        // 扫描二维码/条码回传
        if (resultCode == RESULT_OK) {
            if (data != null) {
                val content = data.getStringExtra(Constant.CODED_CONTENT)?.trim { it <= ' ' }
                LogUtils.e("扫描结果为：$content")
                if (!TextUtils.isEmpty(content)) {
                    scanCode.set(content)
                }
            }
        }

    }

}